{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\model_selection\\_split.py:776: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=5.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best Logistic Regression Model: LogisticRegression(C=100, max_iter=1000, random_state=42)\n",
      "Accuracy of Tuned Logistic Regression: 62.5\n",
      "Cross-Validation Accuracy of LDA: 91.25 ± 0.075\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\model_selection\\_validation.py:540: FitFailedWarning: \n",
      "25 fits failed out of a total of 100.\n",
      "The score on these train-test partitions for these parameters will be set to nan.\n",
      "If these failures are not expected, you can try to debug them by setting error_score='raise'.\n",
      "\n",
      "Below are more details about the failures:\n",
      "--------------------------------------------------------------------------------\n",
      "25 fits failed with the following error:\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\model_selection\\_validation.py\", line 888, in _fit_and_score\n",
      "    estimator.fit(X_train, y_train, **fit_params)\n",
      "  File \"C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\base.py\", line 1473, in wrapper\n",
      "    return fit_method(estimator, *args, **kwargs)\n",
      "  File \"C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 1194, in fit\n",
      "    solver = _check_solver(self.solver, self.penalty, self.dual)\n",
      "  File \"C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 67, in _check_solver\n",
      "    raise ValueError(\n",
      "ValueError: Solver lbfgs supports only 'l2' or None penalties, got l1 penalty.\n",
      "\n",
      "  warnings.warn(some_fits_failed_message, FitFailedWarning)\n",
      "C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\model_selection\\_search.py:1052: UserWarning: One or more of the test scores are non-finite: [       nan 0.12435897 0.31282051 0.45384615        nan 0.10897436\n",
      " 0.45384615 0.5               nan 0.60897436 0.71666667 0.63974359\n",
      "        nan 0.79615385 0.87307692 0.78076923        nan 0.76538462\n",
      " 0.87435897 0.81153846]\n",
      "  warnings.warn(\n",
      "C:\\Users\\pezhm\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages\\sklearn\\model_selection\\_split.py:776: UserWarning: The least populated class in y has only 3 members, which is less than n_splits=5.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import accuracy_score\n",
    "import scipy.io\n",
    "\n",
    "# Load the data from the .mat file\n",
    "data = scipy.io.loadmat(r'C:\\Users\\pezhm\\Desktop\\DESK\\ta\\1.mat')\n",
    "\n",
    "# Extract training data and labels\n",
    "X = data['Data_Train_ML']\n",
    "y = data['Label_Train_ML'].ravel()\n",
    "\n",
    "# Split the data into training and test sets\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# Standardize the features\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "# Hyperparameter tuning for Logistic Regression\n",
    "param_grid = {\n",
    "    'C': [0.01, 0.1, 1, 10, 100],\n",
    "    'penalty': ['l1', 'l2'],\n",
    "    'solver': ['lbfgs', 'liblinear']\n",
    "}\n",
    "\n",
    "logistic_regression_model = LogisticRegression(max_iter=1000, random_state=42)\n",
    "grid_search = GridSearchCV(logistic_regression_model, param_grid, cv=5, scoring='accuracy')\n",
    "grid_search.fit(X_train_scaled, y_train)\n",
    "\n",
    "# Get the best model and evaluate it\n",
    "best_logistic_model = grid_search.best_estimator_\n",
    "y_pred_logistic = best_logistic_model.predict(X_test_scaled)\n",
    "accuracy_logistic = accuracy_score(y_test, y_pred_logistic)\n",
    "\n",
    "# Evaluate LDA with cross-validation\n",
    "lda_model = LinearDiscriminantAnalysis()\n",
    "cv_scores_lda = cross_val_score(lda_model, X, y, cv=5, scoring='accuracy')\n",
    "\n",
    "print(f\"Best Logistic Regression Model: {best_logistic_model}\")\n",
    "print(f\"Accuracy of Tuned Logistic Regression: {accuracy_logistic * 100}\")\n",
    "print(f\"Cross-Validation Accuracy of LDA: {np.mean(cv_scores_lda) * 100 } ± {np.std(cv_scores_lda)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}